package scuba.DiveBuddy.data;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @Author Alex Anderson
 * @Version 0.1
 * @Since 19/01/14.
 *
 * Creates a hook to the database,
 * allowing access to getReadable() and getDatabase() version of the database
 */
public class Database extends SQLiteOpenHelper {
    //Singletons
    private static Database _Instance;
    private static Context _Context;

    public static Database getInstance() {
        if(_Instance == null)
            _Instance = new Database(_Context);

        return _Instance;
    }

    //Constant strings
    public static final String DATABASE_NAME = "DiveDatabase";
    public static final int DATABASE_VERSION = 16;

    //constructor
    public Database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        _Context = context;
    }

    //Methods
    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create the Database
        Log.v("DATABASE", "Creating Database [Version:" + DATABASE_VERSION + "]");

        DiverProfileSource.CreateTable(db);
        AirMixSource.CreateTable(db);
        LogEntrySource.CreateTable(db);
        LogEntryPlanSource.CreateTable(db);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Log.v("DATABASE", "Database Opened [Version:" + DATABASE_VERSION + "]");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int vOld, int vNew) {
        //This method is invoked when the version of the database changes.
        //So Delete (Drop)
        Log.v("DATABASE",
                "Upgrading database from version " + vOld + " to "
                        + vNew + ", which will destroy all old data");

        DiverProfileSource.UpgradeTable(db);
        AirMixSource.UpgradeTable(db);
        LogEntrySource.UpgradeTable(db);
        LogEntryPlanSource.UpgradeTable(db);
    }

    public void Clense() {
        //Completely Drop all tables
        SQLiteDatabase db = getDatabase("Database:Cleanse()");
        DiverProfileSource.UpgradeTable(db);
        AirMixSource.UpgradeTable(db);
        LogEntrySource.UpgradeTable(db);
        LogEntryPlanSource.UpgradeTable(db);
        db.close();
    }

    public SQLiteDatabase getDatabase(String source) {
        Log.v("DATABASE", "[" + source + "] requested getDatabase");
        return this.getWritableDatabase();
    }

    public boolean Exec(String source, String sql)
    {
        Log.v("DATABASE", "Executing SQL: '" + sql + "'");

        try {
            this.getDatabase("Database:Exec()").execSQL(sql);
            return true;
        }
        catch(SQLException e) {
            Log.e("DATABASE", "Exception in ExecSQL. Thrown from '" + source + "'", e);
            return false;
        }
    }

    public void Open() {
        this.getDatabase("Database:Open()");
        //call get readable, this should force the database into existence, be it from create, load or upgrade.
    }

}
